<meta charset="UTF-8">
<link rel="stylesheet" href="__LAY__/css/layui.css" media="all" />
<script type="text/javascript" src="__JS__/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="__LAY__/layui.js"></script>
<style>

</style>
<form class="layui-form layui-form-pane" id="commentForm" method="post" action="{:url('user/userinfo')}" enctype="multipart/form-data">
     <div class="layui-form-item ">
        <label class="layui-form-label">用户账号</label>
        {if $vo and $vo.username}
        <div class="layui-input-block">
            <input  readonly="readonly"  type="text" name="username"  value='{$vo.username|default=""}'  lay-verType="tips"   lay-verify="required" placeholder="请输入角色名称" autocomplete="off" class="layui-input">
             {else}
            <input name="username" value='{$vo.username|default=""}' required="required" title="请输入用户名称" placeholder="请输入用户名称" class="layui-input">
            {/if}
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input  class="man" type="radio" name="sex" value="0" title="男"  checked="" >
            <input  class="female" type="radio" name="sex" value="1" title="女" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-upload">
          <div class="layui-upload-list">
            <img class="layui-upload-img" style="width: 92px;height: 92px;margin: 0 10px 10px 0;" id="headimg" src="{$vo.thumb}">
          </div>
            <button type="button" class="layui-btn" style="margin:-7.5% 0px 0px 27%;" id="upimg">上传图片</button>
            <input type="hidden" name="thumb" value="{$vo.thumb}">
        </div>  
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">电话</label>
        <div class="layui-input-block">
            <input type="text" name="mobile" value="{$vo.mobile}"  lay-verify="phone" required="" title="请输入手机号码" placeholder="请输入手机号码" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="mail" value="{$vo.mail}"  lay-verify="email" required="" title="请输入邮箱" placeholder="请输入邮箱" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" placeholder="请输入内容" class="layui-textarea">{$vo.remark}</textarea>
        </div>
    </div>
    <div class="hr-line-dashed"></div>

    <div class="layui-form-item" style="text-align: center!important;">
        <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
        <a class="layui-btn layui-btn-danger cancel">取消编辑</a>
    </div>

</form>
<script>
    var sex = '{$vo['sex']}';
    if (sex == 0) {
        $(".man").attr("checked","checked");
        $(".female").removeAttr("checked");
    }
    if (sex == 1) {
        $(".female").attr("checked","checked");
        $(".man").removeAttr("checked");
    };
    layui.use(['element', 'layedit','laypage','layer','form','upload'], function(){
        $ = layui.jquery;//jquery
        var form = layui.form
        ,layer = layui.layer
        //监听提交
        form.on('submit(formDemo)', function(data){
          var msg = data.field;
          var url = '{:url('user/userinfo')}';
          $.post(url,msg,function(data){
              if(data.code){
                  layer.msg(data.msg, {
                      icon: 6,//成功的表情
                      time: 2000 //2秒关闭（如果不配置，默认是3秒）
                  }, function(){
                      parent.layer.close(parent.index);
                  }); 
              }else{
                  layer.msg(data.msg,{icon: 5});//失败的表情
                  return;
              }
          });
          return false;//阻止表单跳转
        });

        $(".cancel").click(function() {
            layer.confirm('確定取消?', function(index){
                
                 parent.layer.close(parent.index);
            });
       });

        upload = layui.upload;
        //普通图片上传
        var uploadInst = upload.render({
        elem: '#upimg'
        ,url: '{:url("user/upload")}'
        ,before: function(obj){
          //预读本地文件示例，不支持ie8
          obj.preview(function(index, file, result){
            $('#headimg').attr('src', result); //图片链接（base64）
          });
        }
        ,done: function(res){
          //如果上传失败
          // console.log(res);return false;
          if(res.code == 1){
            return layer.msg(res.msg);
          }else{
           //上传成功
           $("input[name='thumb']").val(res.msg);
           layer.msg('上传成功');

          }
        }
        ,error: function(){
          //演示失败状态，并实现重传
            uploadInst.upload();
        }
        });  
    }); 
</script>
